function constrained_table = summarize_budget_constraint_table(constraint_type,fix_range,covariates,cost,SMC,winter_prevuse,wave,fbase)
% This function summarizes savings under budget constraints. 

% Inputs: 
% (1) constraint_type: fixed budget, or capped budget
% (2) fix_range: small deviation from the fixed budget allowed; can be zero
% (3) covariates: covariate specification
% (4) cost: private cost savings, social cost savings, or energy savings
% (5) SMC: =1 use social marginal cost; =0 use retail electricity price
% (6) winter_prevuse: indicates whether baseline consumption is calculated 
% as the mean of consumption in winter months (Jan and Feb) or as the mean
% of specified pre-treatment periods
% (7) wave: =0 indicating pooled sample
% (8) fbase: string used to indicate the propensity score and baseline
% months specification for output table 

% Outputs: 
% (1) constrained_table: table summarizes savings under budget cosntraints


%% Load previously saved results 
switch winter_prevuse 
    case 1
        s_winter = '_winter';
    case 0
        s_winter = '';
end

matrix_onedim = cell(size(cost,2),1);
matrix_quadrant = cell(size(cost,2),size(covariates,2));
percent_onedim = cell(size(cost,2),1);
savings_onedim = cell(size(cost,2),1);
percent_quadrant = cell(size(cost,2),size(covariates,2));
savings_quadrant = cell(size(cost,2),size(covariates,2));

switch constraint_type 
    case 'fixedshare' 
        for j=1:size(covariates,2) % covariate
            covariate = covariates{j};
            for i=1:size(cost,2) % cost
                if (cost{i})
                    if (SMC{i})
                        s_cost = 'SMC';
                    else
                        s_cost = 'PMC';
                    end
                else
                    s_cost='kwh';
                end
                matrix_quadrant{i,j} = strcat(covariate,'_',s_cost); % save quadrant specification matrix
                matrix_onedim{i,1} = strcat(s_cost); % save univariate specification matrix 
                
                s_range = sprintf('fix%0.2f',fix_range*100);
                filename_coefs=sprintf('coef_fixedshare_onedim_%s_%s%s_wave%1.0f_%s.mat',covariate,s_cost,s_winter,wave,s_range);
                filename = sprintf('%s_%s',fbase,filename_coefs);
                S=load(filename); 
                percent_onedim{i,1}=round(S.percent*100,2);
                savings_onedim{i,1}=round(S.savings,2);

                filename_coefs=sprintf('coef_fixedshare_quadrant_%s_%s%s_wave%1.0f_%s.mat',covariate,s_cost,s_winter,wave,s_range);
                filename = sprintf('%s_%s',fbase,filename_coefs);
                S=load(filename);
                percent_quadrant{i,j}=round(S.percent*100,2);
                savings_quadrant{i,j}=round(S.savings,2);
            end
        end  
    case 'capshare'
        for j=1:size(covariates,2) % covariate
            covariate = covariates{j};
            for i=1:size(cost,2) % cost
                if (cost{i})
                    if (SMC{i})
                        s_cost = 'SMC';
                    else
                        s_cost = 'PMC';
                    end
                else
                    s_cost='kwh';
                end
                matrix_quadrant{i,j} = strcat(covariate,'_',s_cost);
                matrix_onedim{i,1} = strcat(s_cost);
                
                filename_coefs=sprintf('coef_capshare_onedim_%s_%s%s_wave%1.0f.mat',covariate,s_cost,s_winter,wave);
                filename = sprintf('%s_%s',fbase,filename_coefs);
                S=load(filename);
                percent_onedim{i,1}=round((sum(S.nw.*S.in_Ghat)/S.n*S.select_fraction)*100,2);
                savings_onedim{i,1}=round(sum(S.gu.*S.in_Ghat)/S.n*S.select_fraction,2);

                filename_coefs=sprintf('coef_capshare_quadrant_%s_%s%s_wave%1.0f.mat',covariate,s_cost,s_winter,wave);
                filename = sprintf('%s_%s',fbase,filename_coefs);
                S=load(filename);
                percent_quadrant{i,j}=round((sum(S.nw.*S.in_Ghat)/S.n*S.select_fraction)*100,2);
                savings_quadrant{i,j}=round(sum(S.gu.*S.in_Ghat)/S.n*S.select_fraction,2);
            end
        end  
end
%% set up results table (separate for capped and fixed share)

constrained_table=nan(5,9);
constrained_table=array2table(constrained_table,'VariableNames',{'Constraint','Rule','Covariates',...
    'Share-kwh','kwh','Share-PMC','PMC','Share-SMC','SMC'}); % DO NOT CHANGE HEADERS!! USE FOR MATCHING LATER!
format short g
constrained_table.(1) = {'RCT'; constraint_type; constraint_type; constraint_type; constraint_type}; 
constrained_table.(2) = {'Actual RCT';'EWM-univariate';'EWM-quadrant';'EWM-quadrant';'EWM-quadrant'}; % rule column
constrained_table.(3) = {'-';'/';'income';'size';'vintage'}; % covariate column

savings_RCT={-3.63;-0.09;0.32}; % add a RCT row to table 
percent_RCT={72.06;72.06;72.06};

for j=1:size(covariates,2) % covariate
    covariate = covariates{j};
    for i=1:size(cost,2) % cost
        if (cost{i})
            if (SMC{i})
                s_cost = 'SMC';
            else
                s_cost = 'PMC';
            end
        else
            s_cost='kwh';
        end
        
        % fill in quadrant savings 
        table_column_idx=find(strcmp(constrained_table.Properties.VariableNames',s_cost)); 
        table_row_idx=find(strcmp(covariate,constrained_table.Covariates)); 
        find_in_matrix = strcmp(strcat(covariate,'_',s_cost),matrix_quadrant); 
        constrained_table(table_row_idx,table_column_idx) = savings_quadrant(find_in_matrix); 
        % fill in quadrant treat share 
        table_column_idx=find(strcmp(constrained_table.Properties.VariableNames',strcat('Share-',s_cost))); 
        find_in_matrix = strcmp(strcat(covariate,'_',s_cost),matrix_quadrant);
        constrained_table(table_row_idx,table_column_idx) = percent_quadrant(find_in_matrix);        
        
        % fill in univariate savings 
        table_column_idx=find(strcmp(constrained_table.Properties.VariableNames',s_cost));
        table_row_idx=find(strcmp('EWM-univariate',constrained_table.Rule));
        find_in_matrix = strcmp(strcat(s_cost),matrix_onedim);
        constrained_table(table_row_idx,table_column_idx) = savings_onedim(find_in_matrix);
        % fill in univariate treat share 
        table_column_idx=find(strcmp(constrained_table.Properties.VariableNames',strcat('Share-',s_cost)));
        find_in_matrix = strcmp(strcat(s_cost),matrix_onedim);
        constrained_table(table_row_idx,table_column_idx) = percent_onedim(find_in_matrix);
        
        % fill in RCT savings 
        table_column_idx=find(strcmp(constrained_table.Properties.VariableNames',s_cost));
        table_row_idx= 1; % RCT stays in the top row
        find_in_matrix = strcmp(strcat(s_cost),matrix_onedim);
        constrained_table(table_row_idx,table_column_idx) = savings_RCT(find_in_matrix);
        % fill in RCT treat share 
        table_column_idx=find(strcmp(constrained_table.Properties.VariableNames',strcat('Share-',s_cost)));
        find_in_matrix = strcmp(strcat(s_cost),matrix_onedim);
        constrained_table(table_row_idx,table_column_idx) = percent_RCT(find_in_matrix);
    end
end

end